<?php

namespace App\Common\Http\Controller;

use App\ApiBaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Handles\CrmRpcHandle;

class ImController extends ApiBaseController
{
    use CrmRpcHandle;

    /**
     * 表单验证
     * @param $data
     * @param $type
     * @return bool
     * @throws GoodsException
     */
    protected function validate($data, $ruleArr, $source = '')
    {
        $rule = [
            'From_Account' => 'required',
            'To_Account' => 'required',
            'MsgType' => 'required|in:TIMTextElem,TIMLocationElem,TIMFaceElem,TIMCustomElem,TIMSoundElem,TIMImageElem,TIMFileElem,TIMVideoFileElem',
            'Text' => 'required|string|between:1,255',
            'UUID' => 'required|string',
            'ImageFormat' => 'required|integer',
            'ImageInfoArray' => 'required|array',
            'Data' => 'required|string|between:1,100',
            'Desc' => 'required|string|between:1,255',
            'Ext' => 'required|string',
            'MsgKey' => 'required|string',
            'MaxCnt' => 'required|integer',
            'MinTime' => 'required|integer',
            'MaxTime' => 'required|integer',
            'LastMsgKey' => 'nullable|string'
        ];

        $message = [
            'From_Account.required' => '请传入发送人ID',
            'To_Account.required' => '请传入',
            'id.min' => '就诊人ID必须大于0',
            'MsgType.required' => '请传入短信类型参数',
            'MsgType.in' => '短信类型参数异常',
            'LastMsgKey.string' => '最后一次聊天KEY必须为字符串',
            'Text.required' => '请输入内容',
            'Text.string' => '内容异常',
            'Text.between' => '内容请控制在255字内',
            'UUID.required' => '请输入图片唯一值',
            'UUID.string' => '图片唯一值应为字符串',
            'ImageFormat.required' => '请传入图片类型参数',
            'ImageFormat.integer' => '图片类型参数异常',
            'ImageInfoArray.required' => '请传入图片参数集合',
            'ImageInfoArray.array' => '图片参数集合必须是数组',
            'Data.required' => '请设置消息内容',
            'Data.string' => '消息内容必须为字符串',
            'Data.between' => '消息内容请控制在100字内',
            'Desc.required' => '请设置消息描述',
            'Desc.string' => '消息描述必须为字符串',
            'Desc.between' => '消息描述请控制在255字节内',
            'Ext.required' => '请设置消息链接',
            'Ext.string' => '消息链接必须为字符串',
            'MsgKey.required' => '请传入消息序列号',
            'MsgKey.string' => '消息序列号必须为字符串',
            'MaxCnt.required' => '请传入拉取条数',
            'MaxCnt.integer' => '拉取条数必须为数字',
            'MinTime.required' => '请传入拉开始时间',
            'MinTime.integer' => '拉取开始时间必须为数字',
            'MaxTime.required' => '请传入拉取结束时间',
            'MaxTime.integer' => '拉取结束时间必须为数字',
        ];

        // 根据配置选择规则组成
        $rule = array_intersect_key($rule, array_flip($ruleArr));

        $validate = Validator::make($data, $rule, $message);
        if($validate->fails()){
            throw new \Exception($validate->errors()->first());
        }
        return true;
    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/16 6:02 下午
     * Describe: IM拉取用户消息
     * Version: V1.0
     * @param Request $request
     */
    public function search(Request $request)
    {
        // 接受参数
        $params = $request->input();

        $fields = ['To_Account', 'MaxCnt', 'MinTime', 'MaxTime', 'LastMsgKey'];

        try {
            // 校验参数
            $this->validate($params, $fields);

            // 发送人ID
            $params['From_Account'] = isset($params['user_id']) ? 'user_' . $params['user_id'] : 'doctor_' . $params['doctor_id'];
            // 发送时间戳
            $params['MsgTimeStamp'] = time();

            // RPC 拉取消息
            $data = $this->imGetSearch($params);

            return $this->success('获取成功', $data);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/16 5:49 下午
     * Describe: IM消息撤回
     * Version: V1.0
     * @param Request $request
     */
    public function withdraw(Request $request) {
        // 接受参数
        $params = $request->input();

        $fields = ['To_Account', 'MsgKey'];

        try {
            // 校验参数
            $this->validate($params, $fields);

            // 发送人ID
            $params['From_Account'] = isset($params['user_id']) ? 'user_' . $params['user_id'] : 'doctor_' . $params['doctor_id'];
            // 发送时间戳
            $params['MsgTimeStamp'] = time();

            // RPC 撤回消息
            $this->imWithdraw($params);

            return $this->success('撤回成功');
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }

    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/16 3:05 下午
     * Describe: IM发送消息
     * Version: V1.0
     * @param Request $request
     */
    public function sendSms(Request $request) {
        // 接受参数
        $params = $request->input();

        switch ($params['MsgType']) {
            case 'TIMTextElem':
                $fields = ['To_Account', 'MsgType', 'Text'];
                break;
            case 'TIMImageElem':
                $fields = ['To_Account', 'MsgType', 'UUID', 'ImageFormat', 'ImageInfoArray'];
                break;
            case 'TIMCustomElem':
                $fields = ['To_Account', 'MsgType', 'Data', 'Desc', 'Ext'];
                break;
            default:
                return $this->error('暂不支持其他消息格式');
                break;
        }

        try {
            // 校验参数
            $this->validate($params, $fields);

            // 发送人ID
            $params['From_Account'] = isset($params['user_id']) ? 'user_' . $params['user_id'] : 'doctor_' . $params['doctor_id'];
            // 发送时间戳
            $params['MsgTimeStamp'] = time();
            $params['SyncOtherMachine'] = 1;

            // RPC 发送消息
            $this->imSendMessage($params);

            return $this->success('发送成功');
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }
}
